<template>
	<u-search
		placeholder="请输入搜索关键词"
		action-text="搜索"
		:disabled="false"
		width="100%"
		:clearabled="true"
		v-model="searchText"
		@search="JUMP"
		@custom="JUMP"
		@clickIcon="JUMP"
	></u-search>
</template>

<script>
export default {
	data() {
		return {
			searchText: ''
		};
	},
	methods: {
		JUMP() {
			if (this.searchText.trim() !== '') {
				this.setStoreHistoryList(this.searchText.trim());
				uni.navigateTo({
					url: `/pages/search-result-page/search-result-page?search=${this.searchText}`
				});
			} else {
				uni.showToast({
					title: '您未输入任何搜索关键词',
					icon: 'none'
				});
			}
		},

		// 设置历史记录列表
		setStoreHistoryList(searchContent) {
			// 从本地存储中获取历史记录列表
			let historyList = uni.getStorageSync('historyList') || []; //这一句是获取本地存储的数据
			if (!historyList.includes(searchContent)) {
				historyList.unshift(searchContent); //向数组里添加咱们搜索的数据
				uni.setStorageSync('historyList', historyList);
				uni.$emit('updataHistory');
			}
		}
	}
};
</script>

<style></style>
